package cn.vshare.controller.interceptor;

import cn.vshare.dao.CoinDao;
import cn.vshare.util.JsonUtil;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CoinControllerInterceptor extends HandlerInterceptorAdapter {

    @Resource
    private CoinDao coinDao;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session=request.getSession();
        if (session.getAttribute("USER")==null){
            JsonUtil.returnJson(response,"请登录");
            return false;
        }else{
//            rewardToUser(String myId, String userId, int count)
            String myId=request.getParameter("myId");
            String countStr=request.getParameter("count");
            int count=Integer.parseInt(countStr);

            int total=coinDao.queryByUserId(myId).getTotal();
            if (total<count){
                JsonUtil.returnJson(response,"请充值");
                return false;
            }
            return true;
        }
    }

}
